home *** CD-ROM | disk | FTP | other *** search
- /* macstuff.c - macintosh interface routines for xlisp */
-
- #include <qd.h>
- #include <mem.h>
- #include <file.h>
- #include <font.h>
- #include <win.h>
- #include <menu.h>
- #include <dialog.h>
- #include <event.h>
- #include <pack.h>
- #include <stdio.h>
-
- #define TRUE 1
- #define FALSE 0
-
- #define appleid 1
- #define applemenu 0
- #define fileid 256
- #define filemenu 1
- #define editid 257
- #define editmenu 2
- #define controlid 258
- #define controlmenu 3
- #define lastmenu 4
-
- extern char *s_unbound;
-
- #include <qdvars.h> /* quickdraw globals */
- MenuHandle mymenus[lastmenu];
- SFReply loadfile;
- OsType filetypes[] = { {'T','E','X','T'} };
- Rect dragrect;
- int x,y;
-
- /* command window */
- WindowRecord cwrecord;
- WindowPtr cwindow;
-
- /* graphics window */
- WindowRecord gwrecord;
- WindowPtr gwindow;
-
- /* window mode */
- int splitmode;
-
- #define TIMEON 500
- #define TIMEOFF 100
- int cursortime,cursorstate;
-
- /* graphics window */
- #define GHORIZONTAL 4
- #define GVERTICAL 22
- #define GWIDTH 504
- #define GHEIGHT 232
-
- /* split screen */
- #define SSCRW 80
- #define SSCRH 5
- #define SHORIZONTAL 4
- #define SVERTICAL 275
- #define SWIDTH 504
- #define SHEIGHT 64
-
- /* normal screen */
- #define NSCRW 80
- #define NSCRH 24
- #define NHORIZONTAL 4
- #define NVERTICAL 41
- #define NWIDTH 504
- #define NHEIGHT 298
-
- /* screen buffer */
- #define SCRH 24
- #define SCRW 80
- char screen[SCRH*SCRW],*topline,*curline;
- int scrh,scrw;
-
- #define LINEMAX 200
- char linebuf[LINEMAX+1],*lineptr;
- int linepos[LINEMAX],linelen;
-
- #define CHARMAX 100
- char charbuf[CHARMAX],*inptr,*outptr;
- int charcnt;
-
- osinit(name)
- char *name;
- {
- Rect screenrect;
- long cursp;
- char *p;
-
- /* initialize the toolbox */
- InitGraf(&thePort);
- InitFonts();
- InitWindows();
- InitMenus();
- TEInit();
- InitDialogs(0L);
- InitCursor();
-
- /* reserve 32K more for the stack */
- asm { move.l A7,cursp(A6) }
- SetApplLimit(cursp - 32768L);
-
- /* setup the menu bar */
- setupmenus();
-
- /* setup the bounds rectangle for dragging windows */
- SetRect(&dragrect,4,24,508,338);
-
- /* Create the graphics and control windows */
- gwindow = GetNewWindow(129,&gwrecord,-1L);
- cwindow = GetNewWindow(128,&cwrecord,-1L);
-
- /* establish the command window as the current port */
- SetPort(cwindow);
-
- /* setup the font, size and writing mode for the command window */
- TextFont(Monaco); TextSize(9); TextMode(srcCopy);
-
- /* setup command mode */
- splitmode = FALSE;
- scrh = SCRH;
- scrw = SCRW;
- scrclear();
-
- /* disable the Cursor */
- cursorstate = -1;
-
- /* setup the input ring buffer */
- inptr = outptr = charbuf;
- charcnt = 0;
-
- /* setup the Line editor */
- linelen = 0;
-
- /* lock the font in memory (need to write something first) */
- for (p = name; *p; )
- scrputc(*p++);
- scrputc('\r'); scrputc('\n');
- SetFontLock(TRUE);
- }
-
- osfinish()
- {
- }
-
- int osrand(n)
- int n;
- {
- return (rand() % n);
- }
-
- int osgetc(fp)
- FILE *fp;
- {
- if (fp == stdin) return (linegetc());
- else return (getc(fp));
- }
-
- int osputc(ch,fp)
- int ch; FILE *fp;
- {
- oscheck();
- if (fp == stdout) return (lineputc(ch));
- else return (putc(ch,fp));
- }
-
- setupmenus()
- {
- int i;
-
- mymenus[applemenu] = GetMenu(appleid); AddResMenu(mymenus[applemenu],"DRVR");
- mymenus[filemenu] = GetMenu(fileid);
- mymenus[editmenu] = GetMenu(editid);
- mymenus[controlmenu] = GetMenu(controlid);
- for (i = 0; i < lastmenu; i++)
- InsertMenu(mymenus[i],0);
- DrawMenuBar();
- }
-
- int linegetc()
- {
- int ch;
-
- if (linelen--) return (*lineptr++);
- linelen = 0;
- while ((ch = scrgetc()) != '\r')
- switch (ch) {
- case EOF:
- return (linegetc());
- case '\010':
- if (linelen > 0) {
- linelen--;
- while (x > linepos[linelen]) {
- scrputc('\010'); scrputc(' '); scrputc('\010');
- }
- }
- break;
- default:
- if (linelen < LINEMAX) { linebuf[linelen] = ch; linepos[linelen] = x; linelen++; }
- scrputc(ch);
- break;
- }
- scrputc('\r'); scrputc('\n');
- linebuf[linelen] = '\n';
- lineptr = linebuf;
- return (*lineptr++);
- }
-
- int lineputc(ch)
- int ch;
- {
- if (ch == '\n') scrputc('\r');
- scrputc(ch);
- return (1);
- }
-
- scrclear()
- {
- curline = screen;
- for (y = 0; y < SCRH; y++)
- for (x = 0; x < SCRW; x++)
- *curline++ = ' ';
- topline = curline = screen;
- x = y = 0;
- }
-
- int scrgetc()
- {
- CursorOn();
- while (charcnt == 0)
- oscheck();
- CursorOff();
- return (scrnextc());
- }
-
- int scrnextc()
- {
- int ch;
- if (charcnt > 0) {
- ch = *outptr++; charcnt--;
- if (outptr >= &charbuf[CHARMAX])
- outptr = charbuf;
- }
- else {
- charcnt = 0;
- ch = EOF;
- }
- return (ch);
- }
-
- oscheck()
- {
- WindowPtr whichwindow;
- EventRecord myevent;
- long sel;
-
- SystemTask();
- CursorUpdate();
-
- while (GetNextEvent(everyEvent,&myevent))
- switch (myevent.what) {
- case mouseDown:
- switch (FindWindow(&myevent.where,&whichwindow)) {
- case inMenuBar:
- if (sel = MenuSelect(&myevent.where))
- docommand(sel);
- break;
- case inSysWindow:
- SystemClick(&myevent,whichwindow);
- break;
- case inDrag:
- DragWindow(whichwindow,&myevent.where,&dragrect);
- break;
- case inGoAway:
- if (TrackGoAway(whichwindow,&myevent.where))
- exit();
- break;
- case inGrow:
- case inContent:
- if (whichwindow != FrontWindow() && whichwindow != gwindow)
- SelectWindow(whichwindow);
- break;
- }
- break;
- case keyDown:
- case autoKey:
- if (cwindow == FrontWindow()) {
- if (myevent.modifiers & 0x100) {
- if (sel = MenuKey((char)myevent.message))
- docommand(sel);
- }
- else {
- if (charcnt < CHARMAX) {
- *inptr++ = myevent.message & 0xFF; charcnt++;
- if (inptr >= &charbuf[CHARMAX])
- inptr = charbuf;
- }
- }
- }
- break;
- case activateEvt:
- break;
- case updateEvt:
- whichwindow = (WindowPtr) myevent.message;
- BeginUpdate(whichwindow);
- if (whichwindow == cwindow)
- doupdate();
- EndUpdate(whichwindow);
- break;
- }
- }
-
- CursorUpdate()
- {
- if (cursorstate != -1)
- if (cursortime-- < 0) {
- scrposition(x,y);
- if (cursorstate) {
- DrawChar(' ');
- cursortime = TIMEOFF;
- cursorstate = 0;
- }
- else {
- DrawChar('_');
- cursortime = TIMEON;
- cursorstate = 1;
- }
- }
- }
-
- CursorOn()
- {
- cursorstate = cursortime = 0;
- }
-
- CursorOff()
- {
- if (cursorstate == 1) {
- scrposition(x,y);
- DrawChar(' ');
- }
- cursorstate = -1;
- }
-
- scrputc(ch)
- int ch;
- {
- if (ch == '\r') x = 0;
- else if (ch == '\n') { nextline(&curline); if (y < scrh-1) y++; else scrollup(); }
- else if (ch == '\t') { scrputc(' '); while (x & 7) scrputc(' '); }
- else if (ch == '\010') { if (x) x--; }
- else if (ch >= 0x20 && ch < 0x7F) {
- scrposition(x,y);
- DrawChar(ch);
- curline[x] = ch;
- if (x < scrw-1) x++;
- else { x = 0; nextline(&curline); if (y < scrh-1) y++; else scrollup(); }
- }
- }
-
- scrflush()
- {
- lineptr = linebuf; linebuf[0] = '\n'; linelen = 1;
- inptr = outptr = charbuf;
- charcnt = -1;
- }
-
- scrposition(x,y)
- int x,y;
- {
- MoveTo((x * 6) + 4,(y * 12) + 12);
- }
-
- pascal filefilter(pblock)
- paramblkptr pblock;
- {
- char *p; int len;
- p = pblock->ioNamePtr; len = *p++ &0xFF;
- return (len >= 4 && strncmp(p+len-4,".lsp",4) == 0 ? 0 : 0x100);
- }
-
- pascal aboutfilter(theDialog,theEvent,itemHit)
- DialogPtr theDialog; EventRecord *theEvent; int *itemHit;
- {
- return (theEvent->what == mouseDown ? 0x100 : 0);
- }
-
- docommand(themenu,theitem)
- int themenu,theitem;
- {
- DialogRecord mydialog;
- char name[256];
- GrafPtr gp;
- Point p;
- int n;
-
- CursorOff();
- HiliteMenu(themenu);
- switch (themenu) {
- case appleid:
- switch (theitem) {
- case 1:
- GetNewDialog(129,&mydialog,-1L);
- ModalDialog(aboutfilter,&n);
- CloseDialog(&mydialog);
- break;
- default:
- GetItem(mymenus[0],theitem,name);
- GetPort(&gp);
- OpenDeskAcc(name);
- SetPort(gp);
- break;
- }
- break;
- case fileid:
- switch (theitem) {
- case 1: /* load */
- case 2: /* load noisily */
- p.a.h = 100; p.a.v = 100;
- SFGetFile(&p,"",filefilter,-1,filetypes,0L,&loadfile);
- if (loadfile.good) {
- HiliteMenu(0);
- SetVol(0L,loadfile.vRefNum);
- if (xlload(loadfile.fName,1,(theitem == 1 ? 0 : 1)))
- scrflush();
- else
- xlabort("load error");
- }
- break;
- case 4: /* quit */
- exit();
- }
- break;
- case editid:
- switch (theitem) {
- case 1: /* undo */
- case 3: /* cut */
- case 4: /* copy */
- case 5: /* paste */
- case 6: /* clear */
- SystemEdit(theitem-1);
- break;
- }
- break;
- case controlid:
- scrflush();
- HiliteMenu(0);
- switch (theitem) {
- case 1: /* break */
- xlbreak("user break",s_unbound);
- break;
- case 2: /* continue */
- xlcontinue();
- break;
- case 3: /* clean-up error */
- xlcleanup();
- break;
- case 4: /* Cancel input */
- xlabort("input canceled");
- break;
- case 5: /* Top Level */
- xltoplevel();
- break;
- case 7: /* split screen */
- scrsplit(splitmode ? FALSE : TRUE);
- break;
- }
- break;
- }
- HiliteMenu(0);
- CursorOn();
- }
-
- scrsplit(split)
- int split;
- {
- ShowHide(cwindow,0);
- if (split) {
- CheckItem(mymenus[controlmenu],7,1);
- scrh = SSCRH; scrw = SSCRW;
- scrclear();
- MoveWindow(cwindow,SHORIZONTAL,SVERTICAL,1);
- SizeWindow(cwindow,SWIDTH,SHEIGHT,1);
- EraseRect(&cwindow->portRect);
- ShowHide(gwindow,1);
- }
- else {
- CheckItem(mymenus[controlmenu],7,0);
- scrh = NSCRH; scrw = NSCRW;
- scrclear();
- MoveWindow(cwindow,NHORIZONTAL,NVERTICAL,1);
- SizeWindow(cwindow,NWIDTH,NHEIGHT,1);
- EraseRect(&cwindow->portRect);
- ShowHide(gwindow,0);
- }
- ShowHide(cwindow,1);
- splitmode = split;
- }
-
- doupdate()
- {
- char *Line; int y;
- Line = topline;
- for (y = 0; y < scrh; y++) {
- scrposition(0,y);
- DrawText(Line,0,scrw);
- nextline(&Line);
- }
- }
-
- nextline(pline)
- char **pline;
- {
- if ((*pline += SCRW) >= &screen[SCRH*SCRW]) *pline = screen;
- }
-
- scrollup()
- {
- RgnHandle updateRgn;
- int x;
- updateRgn = NewRgn();
- ScrollRect(&cwindow->portRect,0,-12,updateRgn);
- DisposeRgn(updateRgn);
- for (x = 0; x < SCRW; x++)
- topline[x] = ' ';
- nextline(&topline);
- }
-